﻿@model IEnumerable<BaseData.Model.DeliveryMan>
@{
    ViewBag.Title = "配送员管理";
    var idx = 0;
}

@*@Html.Partial("~/Views/Shared/_UserTitle.cshtml")*@

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="row">
            <div class="col-xs-8 col-sm-8 col-md-6 col-lg-10">
                @using (Ajax.BeginForm("Index", new RouteValueDictionary { { "id", "" } }, new AjaxOptions { UpdateTargetId = "tables", HttpMethod = "Get", InsertionMode = InsertionMode.Replace }, new RouteValueDictionary { { "id", "searchForm" }, { "class", "bs-example bs-example-form" }, { "role", "form" } }))
                {
                    <div class="row">

                        <div class="col-lg-5 col-md-7">
                            <div class="input-group">
                                <input type="text" class="form-control" name="key" id="key" placeholder="查询条件：姓名,身份证">
                                <span class="input-group-btn">
                                    <button class="btn btn-default" type="submit">
                                        <span class="glyphicon glyphicon-search"></span>
                                    </button>

                                </span>

                            </div>
                        </div>

                    </div>
                }
            </div>
            <div class=" col-md-1 pull-right">
                <a href="#" data-toggle="modal" data-backdrop="static" data-target="#myModal" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-plus"></span> 新建</a>
            </div>
        </div>

    </div>
    <div class="panel-body">
        <div id="tables">
            @Html.Partial("_DeliveryMenSearchGet", Model)
        </div>


    </div>
</div>




<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">新建配送员</h4>
            </div>
            <div class="modal-body">
                <div class=" form-horizontal">



                    <div class="form-group">
                        <label for="username" class="col-sm-2 control-label">用户姓名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="username"
                                   placeholder="请输入用户姓名">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="idnumber" class="col-sm-2 control-label">身份证</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="idnumber"
                                   placeholder="请输入身份证号">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="mobile" class="col-sm-2 control-label">联系方式</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="mobile"
                                   placeholder="请输入联系方式">
                        </div>
                    </div>



                    <div class="form-group">
                        <label for="area" class="col-sm-2 control-label">所属区域</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="area">
                                <option value="">默认选择</option>
                            </select>
                        </div>
                    </div>

                </div>
            </div>

            <div class="modal-footer">
                <div class="alert alert-danger" style="display:none" id="errmsg">输出数据不合法，请检查重新输入！</div>
                <button type="button" class="btn btn-default" data-dismiss="modal"><span style="cursor:pointer" class="glyphicon glyphicon-remove"></span>关闭</button>
                <button id="save" type="button" class="btn btn-primary">
                    <span class="glyphicon glyphicon-ok"></span> 提交
                </button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myEditModal" tabindex="-1" role="dialog" aria-labelledby="myEditModalLable" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myEditModalLable">修改配送员</h4>
            </div>
            <div class="modal-body">
                <div class=" form-horizontal">
                    <input type="hidden" id="DeliveryManID" />



                    <div class="form-group">
                        <label for="edusername" class="col-sm-2 control-label">用户姓名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edusername"
                                   placeholder="请输入用户姓名">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edidnumber" class="col-sm-2 control-label">身份证</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edidnumber"
                                   placeholder="请输入身份证号">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edmobile" class="col-sm-2 control-label">联系方式</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edmobile"
                                   placeholder="请输入联系方式">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="earea" class="col-sm-2 control-label">所属区域</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="earea">
                                <option value="">默认选择</option>
                            </select>
                        </div>
                    </div>

                </div>
            </div>
            <div class="modal-footer">
                <div class="alert alert-danger" style="display:none" id="ederrmsg">输出数据不合法，请检查重新输入！</div>
                <button type="button" class="btn btn-default" data-dismiss="modal"><span style="cursor:pointer" class="glyphicon glyphicon-remove"></span>关闭</button>
                <button id="esave" type="button" class="btn btn-primary">
                    <span class="glyphicon glyphicon-ok"></span> 提交
                </button>
            </div>
        </div>
    </div>
</div>




@section Scripts {
    <script>
        var authJson = null;
        $(function () {
            $("[data-toggle='tooltip']").tooltip();
            $.getJSON("/api/AreasApi", function (data) {
                var item = "<option value=-1>=请选择=</option>";
                for (var i = 0; i < data.length; i++) {
                    item += ' <option value="' + data[i].AreaID + '">' + data[i].City.CityName + '-' + data[i].AreaName + '</option>';
                }
                $("#area").empty().append(item);
                $("#earea").empty().append(item);
            });

            //新建
            $("#username").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#mobile").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#idnumber").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#area").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });

            //编辑
            $("#edusername").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#edidnumber").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#earea").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#edmobile").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });

            $("#save").bind("click", function () {
                $("#errmsg").css("display", "none");
                var model = {};
                model.DeliveryManName = $("#username").val();
                model.IDNumber = $("#idnumber").val();
                model.Mobile = $("#mobile").val();
                model.AreaID = $("#area").val();

                if (model.DeliveryManName == "") {
                    $("#username").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.IDNumber == "") {
                    $("#idnumber").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.Mobile == "") {
                    $("#mobile").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.AreaID == "-1") {
                    $("#area").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                $.ajax({
                    url: "/DeliveryMen/Create",
                    type: 'POST',
                    data: { jsonstr: JSON.stringify(model) },
                    success: function (data) {
                        if (data == "OK") {
                            location.href = "/DeliveryMen/Index";
                        }
                    },
                    error: function (error) {
                        if (error.status == 500) {
                            $("#errmsg").css("display", "block");
                        }
                    }
                });
            });
        });
        function Edit(id) {
            $.ajax({
                url: "/DeliveryMen/GetForEdit",
                type: 'GET',
                data: { id: id },
                success: function (data) {

                    $("#DeliveryManID").val(data.DeliveryManID);
                    $("#edusername").val(data.DeliveryManName);
                    $("#edidnumber").val(data.IDNumber);
                    $("#edmobile").val(data.Mobile);
                    $("#earea").val(data.AreaID);

                    $('#myEditModal').modal('show');
                }
            });
            $("#esave").bind("click", function () {
                $("#ederrmsg").css("display", "none");
                var model = {};
                model.DeliveryManID = $("#DeliveryManID").val();
                model.DeliveryManName = $("#edusername").val();
                model.IDNumber = $("#edidnumber").val();
                model.Mobile = $("#edmobile").val();
                model.AreaID = $("#earea").val();

                if (model.DeliveryManName == "") {
                    $("#edusername").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.IDNumber == "") {
                    $("#edidnumber").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.Mobile == "") {
                    $("#edmobile").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.AreaID == "-1") {
                    $("#earea").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                $.ajax({
                    url: "/DeliveryMen/Edit",
                    type: 'POST',
                    data: { jsonstr: JSON.stringify(model) },
                    success: function (data) {
                        location.href = "/DeliveryMen/Index";
                    },
                    error: function (error) {
                        if (error.status == 500) {
                            $("#ederrmsg").css("display", "block");
                        }
                    }
                });
            });
        }
        function Delete(id) {
            if (confirm("提示：以下操作将会删除该配送员信息，是否继续?")) {
                $.ajax({
                    url: "/DeliveryMen/Delete",
                    type: 'GET',
                    data: { id: id },
                    success: function (data) {
                        location.href = "/DeliveryMen/Index";
                    }
                });
            }
        }
    </script>
}
